<?php
class xslt_helper
{

    /**
     * @var _XMLParseMode = {file|mem}
     */
    var $_XMLParseMode = 'file';

    function set_XMLParseMode($new_value)
    {
        if ('file' != $new_value && 'mem' != $new_value)
        {
            throw new Exception('XMLParseMode '.$new_value.' ist unbekannt');
        }
        $this->_XMLParseMode = $new_value;
    }

    function get_XMLParseMode()
    {
        return $this->_XMLParseMode;
    }


    var $config = array (
    'xsltProcXMLBaseDir'=>"c:/user/xampp/htdocs/xsl/",
    'xsltProcXSLBaseDir'=>"c:/user/xampp/htdocs/xsl/",
    'xsltProcRegisterPHPFunctions'=>'off'
    );

    function getConf($key)
    {
        return $this->config[$key];
    }
    /**
     * Render the $xmlFile using $xslFile with the paremters $param
     * @return null if not rendered
     * @param $xmlFile string
     * @param $xslFile string
     * @param $param array
     */
    function renderXML($xmlFile, $xslFile, $param)
    {
//    	print_r(str_replace('<',"&lt;",$xmlFile));
        if ( isset ($param['xmlBaseDir']))
        {
            $xmlBaseDir = $param['xmlBaseDir'];
            unset ($param['xmlBaseDir']);
        } else
        {
            $xmlBaseDir = $this->getConf('xsltProcXMLBaseDir');
        }

        if ( isset ($param['xslBaseDir']))
        {
            $xslBaseDir = $param['xslBaseDir'];
            unset ($param['xslBaseDir']);
        } else
        {
            $xslBaseDir = $this->getConf('xsltProcXSLBaseDir');
        }
        $doPHP = false;
        if ( isset ($param['usePHP']))
        {
            $doPHP = $param['usePHP'];
            unset ($param['usePHP']);
            $doPHP = $doPHP == 'on' || $doPHP == 'ja' || $doPHP == 'yes' || $doPHP == 'true';
        }
        $xmlBaseDir = "file:///".$xmlBaseDir;
        $xslBaseDir = "file:///".$xslBaseDir;
		
        $this->createXSLDocument($xslBaseDir.$xslFile);
        if ('file' == $this->_XMLParseMode)
        {
            $this->createXMLDocument($xmlBaseDir.$xmlFile);
        } elseif ('mem' == $this->_XMLParseMode)
        {
            $this->createXMLDocumentFromMem($xmlFile);
        } else
        {
            throw new Exception("Keine XML Daten vorhanden");
        }
        if ($this->proc == null)
        {
            $this->initXSLTProc();
            if ($doPHP == true)
            {
                $this->setXSLTRegisterPHPFunctions();
            }
        }
        $this->setXSLTParameter($param);
        //>PHP5.3	$this->proc->setProfiling('profiling.txt');
        $this->proc->importStylesheet($this->xslDoc);
        $res = $this->proc->transformToXML($this->xmlDoc);
        if ($res == false) {
            return null;
        } else {
            return $res;
        }
    }



    /**
     * Beinhaltet den XSLTProcessor
     */
    var $proc = null;
    /**
     * Initialisiert den XSLTProcessor und registriert evtl die PHP-Funktionen (aus der Konfiguration).
     *
     */
    function initXSLTProc()
    {
        global $conf;

        $this->proc = new XSLTProcessor();
        $doPHP = $this->getConf('xsltProcRegisterPHPFunctions');
        if ($doPHP)
        {
            $this->setXSLTRegisterPHPFunctions();
        }
    }

    /**
     *
     * @param $param array Felder der Parameter für den XSLT-Processor
     */
    function setXSLTParameter($param)
    {
        foreach ($param as $key=>$val)
        {
            $this->proc->setParameter('', $key, $val);
        }
    }
    /**
     * Registiert die PHP-Function beim XSLTProcessor.
     */
    function setXSLTRegisterPHPFunctions()
    {
        $this->proc->registerPHPFunctions();
    }
    /**
     * Beinhaltet das Stylesheet.
     */
    var $xslDoc = null;
    /**
     * Erstellt ein DOMDocument aus der Stylesheetdatei.
     * @param $FQxslFile String Kompletter Pfad zum Stylesheet
     */
    function createXSLDocument($FQxslFile)
    {
        if ($this->xslDoc == null)
        {
            $this->xslDoc = new DOMDocument();
        }
        $this->xslDoc->load($FQxslFile);
    }
    /**
     * Beinhaltet die XML-Datei
     */
    var $xmlDoc = null;
    /**
     * Erstellt ein DOMDocument aus der XML-Datei.
     * @param $FQxmlFile String Kompletter Pfad zur XML-Datei.
     */
    function createXMLDocument($FQxmlFile)
    {
        if ($this->xmlDoc == null)
        {
            $this->xmlDoc = new DOMDocument();
        }
        $this->xmlDoc->load($FQxmlFile);
    }

    /**
     * Creates the xmlDoc using an XML-String.
     *
     * @param object $xmlString Hopefully valid XML
     */
    function createXMLDocumentFromMem($xmlString)
    {
        if ($this->xmlDoc == null)
        {
            $this->xmlDoc = new DOMDocument();
        }
        $this->xmlDoc->loadXML($xmlString);

    }
} // class
